package AdamsDevice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;
import javax.swing.JOptionPane;

public class DiscoverServicesRemote {


   public static final String SERVICE_TYPE = "_film_streaming._udp.local.";
   public static final String SERVICE_TYPE2 = "_display_device._tcp.local.";

   static class SampleListener implements ServiceListener {
      public void serviceAdded(final ServiceEvent event) {
         System.out.println("Service added   : " + event.getName() + "." + event.getType());
         // The following line is required to get all information associated
         // with a service registration - not just the name and type - for
         // example, the port number and properties. Notification is sent to the
         // serviceResolved(...) method which the request has been completed.
         event.getDNS().requestServiceInfo(event.getType(), event.getName(), 0);
      }

      public void serviceRemoved(ServiceEvent event) {
         System.out.println("Service removed : " + event.getName() + "." + event.getType());
      }

      public void serviceResolved(ServiceEvent event) {
         // Display some information about the service.
         System.out.println("Service resolved: " + event.getInfo().getName() + ", host: "
               + event.getInfo().getHostAddress() + ", port: " + event.getInfo().getPort());
         
        //sets up the new modified TCPSocketClient
         TCPSocketClient chatter = new TCPSocketClient(event.getInfo().getHostAddress(),event.getInfo().getPort());
         String msg = JOptionPane.showInputDialog("Movies | TvShows");
         chatter.sendMessage(msg);
      }
   }
   

   public static void main(String[] args) throws IOException {
      // Again, you can specify which network interface you would like to browse
      // for services on; see commented line.
      // final JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
      final JmDNS jmdns = JmDNS.create();

      // Work the magic: this is where the service listener is registered.
      jmdns.addServiceListener(SERVICE_TYPE, new SampleListener());
      System.out.println("looking for service of type: "+ SERVICE_TYPE);
      System.out.println("---------------------------------------------------------------");
      jmdns.addServiceListener(SERVICE_TYPE2, new SampleListener());
      System.out.println("looking for service of type: "+ SERVICE_TYPE2);

      System.out.println("Press enter to quit");
      new BufferedReader(new InputStreamReader(System.in)).readLine();
      jmdns.close();
      System.exit(0);
   }
}
